#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "myh/MyLog.hpp"
#include "udpclient.hpp"
void HandleError(const std::string &funname, int _erron)
{
    if (_erron < 0)
    {
        lg(Error, "[%s-%d-%s]%s", __FILE__, __LINE__, funname, strerror(errno));
    }
}
void test1(uint16_t port,const std::string& ip)
{
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    HandleError("socket", sockfd);
    struct sockaddr_in clientaddr;
    bzero(&clientaddr, sizeof(clientaddr));
    clientaddr.sin_family = AF_INET; // 2 //AF_INET 9
    clientaddr.sin_port = htons(port);
    int ret1 = inet_pton(AF_INET, ip.c_str(), &clientaddr.sin_addr);
    HandleError("inet_pton", ret1);

    size_t len = sizeof(clientaddr);
    std::string s;
    while (true)
    {
        std::cout << "请发送:";
        std::getline(std::cin, s);

        int ret2 = sendto(sockfd, s.c_str(), s.size(), 0, (const sockaddr *)&clientaddr, len);
        HandleError("sendto", ret1);
    }
}
void Explain()
{
    std::cout << "./client [ip] [port]" << std::endl;
}
void test2(uint16_t port,const std::string& ip)
{
    udpclient::Getinitance(port, ip);

    udpclient::Getinitance()->run();
}

int main(int num, char *args[])
{
    lg.ChangeFilePath(Screnn);
    if (num != 3)
    {
        Explain();
        exit(0);
    }
    uint16_t port = std::stol(args[2]);
    std::string ip = args[1];

    std::cout << port  << std::endl;
    std::cout << ip << std::endl;
    //test1(port,ip);
    test2(port,ip);

    return 0;
}